# encoding: UTF-8

"""
market策略
"""


from base.ctaBase import *
from strategy.ctaTemplate import CtaTemplate


########################################################################
class MarketStrategy(CtaTemplate):
    """Market策略"""
    className = 'Market'
    author = u'何雨'

    # 策略变量
    bar = None
    barMinute = EMPTY_STRING

    initDays = 7
    hisBarOpen = []
    hisBarClose = []
    hisBarHigh = []
    hisBarLow = []

    hisTick = []

    pos = 0

    limitProfit = 20
    limitLost = 10

    orderSlippage = 1

    currTime = ""


    #----------------------------------------------------------------------
    def __init__(self, ctaEngine, setting):
        """Constructor"""
        super(MarketStrategy, self).__init__(ctaEngine, setting)
        
    #----------------------------------------------------------------------
    def onInit(self):
        """初始化策略（必须由用户继承实现）"""
        self.writeCtaLog(u'Market策略初始化')
        
        initData = self.loadBar(self.initDays)
        for bar in initData:
            self.onBar(bar)
        
        self.putEvent()
        
    #----------------------------------------------------------------------
    def onStart(self):
        """启动策略（必须由用户继承实现）"""
        self.writeCtaLog(u'Market策略启动')
        self.putEvent()
    
    #----------------------------------------------------------------------
    def onStop(self):
        """停止策略（必须由用户继承实现）"""
        self.writeCtaLog(u'Market策略停止')
        self.putEvent()
        
    #----------------------------------------------------------------------
    def onTick(self, tick):
        """收到行情TICK推送（必须由用户继承实现）"""

        super(MarketStrategy, self).onTick(tick)
        #print len(self.hisBar)

    #----------------------------------------------------------------------
    def onBar(self, bar):
        """收到Bar推送（必须由用户继承实现）"""

        #记录历史数据
        self.hisBarClose.append(bar.close)
        self.hisBarHigh.append(bar.high)
        self.hisBarOpen.append(bar.open)
        self.hisBarLow.append(bar.low)

        #print self.hisBar
                
        # 发出状态更新事件
        self.putEvent()
        
    #----------------------------------------------------------------------
    def onOrder(self, order):
        """收到委托变化推送（必须由用户继承实现）"""
        # 对于无需做细粒度委托控制的策略，可以忽略onOrder
        pass
    
    #----------------------------------------------------------------------
    def onTrade(self, trade):
        """收到成交推送（必须由用户继承实现）"""
        # 对于无需做细粒度委托控制的策略，可以忽略onOrder
        pass
    

